package connection;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import business.BusinessVideo;

import com.csvreader.CsvReader;

public class ConnectionManagerLibCVS {
	private static ConnectionManagerLibCVS cm;
	private CsvReader csvReader;
	
	public ConnectionManagerLibCVS(String nombFich)
	{		
		csvReader = null;
		try {
				File fichero = new File(nombFich);
				FileReader freader = new FileReader(fichero);
				csvReader = new CsvReader(freader);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	/**
	 * If there is not a ConnectionManager instance, create one.
	 * If yes, then return the instance.
	 */
	public static synchronized ConnectionManagerLibCVS getInstance(String nombFich) {
		cm = new ConnectionManagerLibCVS(nombFich);		
		return cm;
	}
	/**
	 * open one connection with the database
	 */
	public CsvReader openConnection(){
		return csvReader;
	}
	
	/**
	 * close connection with the database
	 */
	public void closeConnection(){
		if (csvReader != null) {
			csvReader.close();
		}
	}
	
	public static void main (String [] args)
	{
		System.out.println(BusinessVideo.getAllVideos());
		System.out.println(BusinessVideo.getAllVideosByGenre("Reality"));
		System.out.println(BusinessVideo.getAllVideosStartingWithLetter('a'));
		System.out.println(BusinessVideo.getVideoByTitle("amazing spiderman"));
	}
}
